//
//  ZYCProfileFaceStudentEntyAlbumViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/28.
//

#import "ZYCProfileFaceStudentEntyAlbumViewController.h"
#import "ZYCUserLoginModel.h"
@interface ZYCProfileFaceStudentEntyAlbumViewController ()<ZYCProfileFaceEntryViewDelegate>

@property (nonatomic, strong) ZYCProfileFaceEntryView *faceEntryView;
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, assign) ZYCLoginRoleType roleType;
@property (nonatomic, copy) UIImage *faceImage;
@property (nonatomic, copy) NSString *customizePath;
@end

@implementation ZYCProfileFaceStudentEntyAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"学生人脸录入";
    [self.view addSubview:self.faceEntryView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
}

- (void)setHeaderImage:(UIImage *)headerImage{
    _headerImage = headerImage;
    self.faceEntryView.kuangImageView.image = headerImage;
}


- (void)clickRightItem:(UIButton *)sender{
    [self postFaceToServiceByStudent];
}
#pragma mark - ZYCProfileFaceEntryViewDelegate
// 确认上传
- (void)profileFaceEntryViewClickComfirm{
    if (self.headerImage) {
        [self uploadFaceImageWithImage:self.headerImage];
    }else{
        [MrjToast makeToast:@"请选择人脸图片" makeView:self.view];
    }
}

#pragma mark - getters
- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [[UIButton alloc] init];
        [_rightButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        [_rightButton setTitle:@"保存" forState:UIControlStateNormal];
        _rightButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 15.0f);
        [_rightButton addTarget:self action:@selector(clickRightItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (ZYCProfileFaceEntryView *)faceEntryView {
    if (!_faceEntryView) {
        _faceEntryView = [[ZYCProfileFaceEntryView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
        _faceEntryView.delegate = self;
    }
    return _faceEntryView;
}



#pragma mark - 网络请求
- (void)uploadFaceImageWithImage:(UIImage *)image{
    [MrjSVProgressHUD showWithStatus:@"人脸上传中..."];
    NSString *urlStr = [NSString stringWithFormat:@"%@/zd-system-service/%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],Common_Post_Upload];
    [[URLManager sharedURLManager] POST:urlStr parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSString *fileName;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"MMddHHmmss";
        NSString *dateString = [formatter stringFromDate:[NSDate date]];
        fileName = [NSString stringWithFormat:@"%@.png",dateString];//以这种格式防止上传的图片重复覆盖
        NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
        [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:@"头像上传成功"];
                self.customizePath = responseObject[@"body"][@"url"];
            }else{
                [MrjToast makeToast:@"头像上传失败" makeView:self.view];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MrjToast makeToast:@"头像上传失败" makeView:self.view];
        }];

}



- (void)postFaceToServiceByStudent{
    if (self.customizePath.length == 0) {
        [MrjToast makeToast:@"请录入人脸" makeView:self.view];
        return;
    }
    NSDictionary *params = @{
        @"customizePath":self.customizePath,
        @"id":self.studentId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Profile_Post_AddStudentCustomize requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            [MrjSVProgressHUD showSuccessWithStatus:responseObject[@"message"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
